<?php

class ForDeliveryController extends BaseController {


	// Table
	public $table = 'order';

	// Eloquent Model
	public $model = 'Order';

	// Base Route
	public $base = 'for-delivery';

	// Localization
	public $local = 'en';

	// Language Name
	public $language = 'for-delivery';

	// Breadcrumbs
	public $breadcrumbs = [ ['link' => 'for-delivery' , 'title' => 'For Delivery'] ];

	// Set List Data - Input, Object
	public function setListData($data, $object) {
		// Sub-Query that get the total count of Received Order Products under Order 
		$sub1 = OrderProduct::whereRaw( 'ORDER_id = order.id AND order_product.archived = 0 AND order_product.status IN ( ' . Constant::ORDER_RECEIVED . ',' . Constant::ORDER_FOR_DELIVERY .' )' )->select( DB::raw('COUNT(*) AS `count`') );

		// Sub-Query that get the total count of Order Products under Order
		$sub2 = OrderProduct::whereRaw( 'ORDER_id = order.id AND order_product.archived = 0 AND order_product.status NOT IN ( ' . Constant::ORDER_CANCELLED . ' )'  )->select( DB::raw('COUNT(*) AS `count`') );

		// Check if the total number of Received Order Products is equal to total number of Order Products under Order
		$object = $object->whereRaw( ' ( ' . $sub1->toSql() . ' ) = ( ' . $sub2->toSql(). ' )' );

		// Check if total number Order Products under Order is Greater than 0
		$object = $object->whereRaw( ' (' . $sub2->toSql() . ') > 0' );
		
		//$update = $object->lists('id');
		
		if( !empty($data['filter_order_id']) )
			$object = $object->where('id', '=', $data['filter_order_id']);
	
		//$this->_updateStatus( $update );
		
		return $object;
	}
	
	public function addListData( $data ) {
		$update_status = $this->model->where('status','<>', Constant::ORDER_FOR_DELIVERY);
		$order_ids = $update_status->lists('id');
		
		if( $update_status->count() ){
			OrderProduct::where('status','<>', Constant::ORDER_FOR_DELIVERY)
			->whereIn('ORDER_id', $order_ids)
			->update(array('status'=> Constant::ORDER_FOR_DELIVERY));
		
			Order::whereIn('id', $order_ids)->update(array('status'=> Constant::ORDER_FOR_DELIVERY));
		}
	
		return $data;
	}

	/*private function _updateStatus( $ids ){
	
		$update_status = Order::whereIn('id', $ids)->where('status','<>', Constant::ORDER_FOR_DELIVERY);
		$order_ids = $update_status->lists('id');
		if( $update_status->count() ){
			OrderProduct::where('status','<>', Constant::ORDER_FOR_DELIVERY)
				->whereIn('ORDER_id', $order_ids)
				->update(array('status'=> Constant::ORDER_FOR_DELIVERY));
				
			Order::whereIn('id', $order_ids)->update(array('status'=> Constant::ORDER_FOR_DELIVERY));
		}
	}*/
	
	// Process before storing data
	public function preStore($data) {

		// Updated At Rules
		$this->rules['updated_at'][] = 'required';

		if($data['updated_at'] == '') {
			$this->error['updated_at'][] = 'Please Enter Updated At Field';
		}

		return $data;
	}


	// Process before updating data
	public function preUpdate($data) {

		// Updated At Rules
		$this->rules['updated_at'][] = 'required';

		if($data['updated_at'] == '') {
			$this->error['updated_at'][] = 'Please Enter Updated At Field';
		}
		
		return $data;
	}
	
	// Process before updating data
	public function generatePicklist() {
		
		$ids = explode(',', Input::get('order_product_ids') );
		
		//$order_picklist=OrderProduct::whereIn('id',$ids);
		//dd($order_picklist);
		
		$get_order=OrderProduct::select('id', 'ORDER_id','name','model')->whereIn('id',$ids);
		$get_so['picklist']=$get_order->get();
		//$get_so['is_print'] = (bool) $print;
		
		return View::make( 'export.fordelivery_picklist' )->with( $get_so );
		
		
	}

}